package com.jingtuo.android.page;

import java.util.ArrayList;

import android.app.ActionBar;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Environment;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.View;

import com.jingtuo.android.R;
import com.jingtuo.android.common.Constants;
import com.jingtuo.android.common.app.BaseActivity;
import com.jingtuo.android.common.app.BaseFragment;
import com.jingtuo.android.common.db.ExceptionInfo;
import com.jingtuo.android.common.db.OnSqliteListener;
import com.jingtuo.android.common.db.SqliteConfig;
import com.jingtuo.android.common.db.SqliteHelper;
import com.jingtuo.android.fragment.NavigationFragment;
import com.jingtuo.android.fragment.NavigationFragment.OnNavgationFragmentListener;
import com.jingtuo.android.fragment.PicturesFragment;

public class HomePage extends BaseActivity implements OnNavgationFragmentListener, DrawerListener{
	
	public static final String TAG = HomePage.class.getName();
	
	private DrawerLayout drawerLayout;
	
	private NavigationFragment nf;
	
	private View navigationView;
	
	private FragmentManager fm;
	
	private ActionBar actionBar;
	
	private String currentTitle;
	
	private BaseFragment currentFragment;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.home);
		init();
	}
	
	public void init(){
		SqliteConfig config = new SqliteConfig();
		config.setContext(this);
		config.setPath(Environment.getExternalStorageDirectory().getAbsolutePath() + "/AndroidLearn/db/test");
		config.setVersion(1);
		SqliteHelper.create(config, new OnSqliteListener() {
			
			@Override
			public void onUpdate(SQLiteDatabase database, int oldVersion, int newVersion) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void onCreate(SQLiteDatabase database) {
				// TODO Auto-generated method stub
				SqliteHelper.createTable(database, ExceptionInfo.class);
			}
		});
		mPreferences.edit().putString(Constants.KEY_DB_PATH, config.getPath()).commit();
		fm = getFragmentManager();
		actionBar = getActionBar();
		actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
		actionBar.setHomeButtonEnabled(true);
		
		ArrayList<String> titles = new ArrayList<String>();
		titles.add(getString(R.string.title_picture));
		titles.add(getString(R.string.title_multi_touch));
		
		currentTitle = titles.get(0);
		actionBar.setTitle(currentTitle);
		
		drawerLayout = (DrawerLayout)findViewById(R.id.home_drawer_layout);
		navigationView = findViewById(R.id.home_navigation);
		
		FragmentTransaction ft = fm.beginTransaction();
		nf = new NavigationFragment();
		nf.setOnNavgationFragmentListener(this);
		Bundle args = new Bundle();
		args.putStringArrayList(NavigationFragment.KEY_TITLES, titles);
		nf.setArguments(args);
		ft.add(R.id.home_navigation, nf, NavigationFragment.class.getName());
		
		currentFragment = new PicturesFragment();
		ft.add(R.id.home_container, currentFragment, currentTitle);
		ft.commit();
		drawerLayout.setDrawerListener(this);
	}

	@Override
	public void onClick(NavigationFragment nf, String title) {
		// TODO Auto-generated method stub
		//close drawer
		drawerLayout.closeDrawer(navigationView);
		//替换标题
		if(!title.equals(currentTitle)){
			if(title.equals(getString(R.string.title_picture))){
				FragmentTransaction ft = fm.beginTransaction();
				PicturesFragment fragment = (PicturesFragment)fm.findFragmentByTag(title);
				if(fragment==null){
					fragment = new PicturesFragment();
					ft.add(R.id.home_container, fragment, title);
				}else{
					ft.show(fragment);
				}
				ft.hide(currentFragment);
				ft.commit();
				currentFragment = fragment;
				currentTitle = title;
				actionBar.setTitle(currentTitle);
			}
		}
	}
	
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		if(drawerLayout.isDrawerOpen(navigationView)){
			drawerLayout.closeDrawer(navigationView);
		}else{
			super.onBackPressed();
		}
	}

	@Override
	public void onDrawerClosed(View arg0) {
		// TODO Auto-generated method stub
		actionBar.setTitle(currentTitle);
	}

	@Override
	public void onDrawerOpened(View arg0) {
		// TODO Auto-generated method stub
		actionBar.setTitle(getString(R.string.app_name));
	}

	@Override
	public void onDrawerSlide(View arg0, float arg1) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onDrawerStateChanged(int arg0) {
		// TODO Auto-generated method stub
		
	}

	
}
